package com.usiful.utils;

import java.io.File;
import java.nio.file.Paths;

/**
 * 路径工具类
 * @author [周织能]
 *
 */
public class PathUtil {

	/**
	 * 获取路径分隔符,只判断是否windows环境下
	 * 
	 * @return
	 */
	public static String getDelimite() {
		String sep = "";
		if (System.getProperties().getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1) {
			sep = "\\";
		} else {
			sep = "/";
		}
		return sep;
	}

	/**
	 * 根据指定文件获取路径后缀
	 * 
	 * @param fullPath
	 * @param lastDir
	 * @return
	 */
	public static String getBackPath(String fullPath, String lastDir) {
		String path = "", tmp, sep = getDelimite();
		lastDir = lastDir.replaceAll(sep, "");
		int idx = fullPath.lastIndexOf(lastDir);
		if (idx != -1) {
			tmp = fullPath.substring(idx + lastDir.length(), fullPath.length());
			String[] pathItems = tmp.split(sep);
			for (int i = 0; i < pathItems.length; i++) {
				path += pathItems[i];
				if (i != 0 && i != pathItems.length - 1) {
					path += sep;
				}
			}
		}
		return path;
	}

	/**
	 * 获取文件全路径
	 * 
	 * @param prevPath
	 * @param lastDir
	 * @return
	 */
	public static String getFullPath(String path,String... subpath) {
		String fullPath = "";
		if(path != null && path != "") {
			fullPath = Paths.get(path, subpath).toString();
		}
		return fullPath;
	}

}
